package com.easy.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		HttpServletResponse resp = (HttpServletResponse)response;
		//设置Http头标
		resp.setHeader("contentType", "text/html;charset=utf-8");
		//加上下面这句才能链式运行下去
		chain.doFilter(request, response);
	}
	
    public CharacterEncodingFilter() {
    	System.out.println("-------------实例化过滤器对象");
    }

	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("-------------初始化过滤器对象");
	}
	
	public void destroy() {
		System.out.println("-------------销毁过滤器对象");
	}

}